//
//  orderRemarkViewController.m
//  Recycling
//
//  Created by jzd on 2018/6/2.
//  Copyright © 2018年 宋佳. All rights reserved.
//

#import "orderRemarkViewController.h"
#import "orderRemarkTableViewCell.h"

@interface orderRemarkViewController ()<UITextViewDelegate,UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableV;
@property (weak, nonatomic) IBOutlet UILabel *toastL;
@property (weak, nonatomic) IBOutlet UITextView *reasonText;
@property (strong,nonatomic) NSMutableArray *array;
@end

@implementation orderRemarkViewController
-(NSMutableArray *)array{
    if (!_array) {
        _array = [NSMutableArray array];
    }
    return _array;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = [UIColor whiteColor];
    self.reasonText.layer.borderColor = [UIColor darkGrayColor].CGColor;
    
    [self.tableV registerNib:[UINib nibWithNibName:NSStringFromClass([orderRemarkTableViewCell class]) bundle:nil] forCellReuseIdentifier:@"cell"];
    //获取工单师傅备注列表
    [self getOrderList];
}
-(void)getOrderList{
    NSDictionary *params = @{@"id":@(self.model.ID)};
    [[SJNetWorkTool sharedTool]requestWithURLString:getOrderNoteList parameters:params method:GET callBack:^(id responseObject, NSError *error) {
        if (responseObject) {
            int code = [[responseObject objectForKey:@"Code"] intValue];
            NSString *msg = [responseObject objectForKey:@"ErrorMessage"];
            id data = [responseObject objectForKey:@"Data"];
            if (code == 1) {
                if (![data isKindOfClass:[NSNull class]]) {
                    NSArray *arr = [data objectForKey:@"rows"];
                    if (arr.count != 0) {
                        self.array = arr.mutableCopy;
                    }
                }
            }else{
                [self.navigationController.view showToast:msg completion:nil];
            }
        }
        [self.tableV reloadData];
    }];
}
- (IBAction)uploadOnClick:(UIButton *)sender {
    NSDictionary *dict = @{@"id":@(self.model.ID),
                           @"uid":@([UserInfoTool shareUserInfoTool].Info.ID),
                           };
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithDictionary:dict];
    if (self.reasonText.text.length != 0) {
        [params setObject:self.reasonText.text forKey:@"note"];
    }
    [[SJNetWorkTool sharedTool]requestWithURLString:SubOrderNote parameters:params method:POST callBack:^(id responseObject, NSError *error) {
        if (responseObject) {
            int code = [[responseObject objectForKey:@"Code"] intValue];
            if (code == 1) {
                SJLog(@"提交备注成功");
                if (self.isSuccess) {
                    self.isSuccess(YES);
                }
            }else{
                SJLog(@"提交备注失败");
                if (self.isSuccess) {
                    self.isSuccess(NO);
                }
            }
        }else{
            if (self.isSuccess) {
                self.isSuccess(NO);
            }
        }
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
    
}
-(void)textViewDidChange:(UITextView *)textView{
    if (textView.text.length>0) {
        self.toastL.hidden = YES;
    }else{
        self.toastL.hidden = NO;
    }
}
//这个函数的最后一个参数text代表你每次输入的的那个字，所以：
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if ([text isEqualToString:@"\n"]){ //判断输入的字是否是回车，即按下return
        //在这里做你响应return键的代码
        [textView resignFirstResponder];
        return NO; //这里返回NO，就代表return键值失效，即页面上按下return，不会出现换行，如果为yes，则输入页面会换行
    }
    
    return YES;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.array.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dict = [self.array objectAtIndex:indexPath.row];
    orderRemarkTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.nameL.text = [dict objectForKey:@"WorkerName"];
    cell.timeL.text = [dict objectForKey:@"CreateDate"];
    cell.reasonL.text = [dict objectForKey:@"Note"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
-(void)dealloc{
    SJLog(@"师傅备注挂了");
}
@end
